package com.easy;

import java.util.Stack;

/**
 * Created by Lxk on 2019/11/24.
 */
public class Solution1021 {

    public String removeOuterParentheses(String S) {
        if (S == null || S.length() == 0) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        Stack<Character> stack = new Stack<>();
        for (Character c : S.toCharArray()) {
            if (c.equals('(')) {
                stack.push(c);
                if (stack.size() > 1) {
                    sb.append(c);
                }
            } else {
                if (stack.size() > 1) {
                    stack.pop();
                    sb.append(c);
                } else {
                    stack.pop();
                }
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        Solution1021 solution1021 = new Solution1021();
        System.out.println(solution1021.removeOuterParentheses("()()"));
    }

}
